﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

using nplJobsCommon.Common;

namespace nplJobsDAL.Select
{
    /// <summary>
    /// Select nationalities from the database
    /// </summary>
    public class SelectNationality : DataAccessBase
    {
        public SelectNationality()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectNationality.ToString();
        }

        #region GetNationality
        public List<Nationality> GetNationality()
        {
            DataBaseHelper dbHelper;
            DataSet dsNationality;

            try
            {
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dsNationality = dbHelper.Run(base.ConnectionString);
                return GetList(dsNationality);
            }
            catch (Exception exp)
            {
                throw new Exception(exp.Message.ToString());
            }
        } 
        #endregion

        #region GetList
        private List<Nationality> GetList(DataSet dsNationality)
        {
            List<Nationality> nationalities;
            try
            {
                nationalities = (from n in dsNationality.Tables[0].AsEnumerable()
                                 select new Nationality
                                 {
                                     ID = n.Field<int>("ID"),
                                     NationalityName = n.Field<string>("NationalityName")
                                 }).ToList();

                return nationalities;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        } 
        #endregion
    }
}
